<script setup lang="ts">
import type { ArticleItem, ArticleResData } from '@/types/data'
import axios from 'axios'
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'

// const props = defineProps<{
//   channelID: number
// }>()
import { useChannelStore } from '@/stores/channel'
const store = useChannelStore()
//d定义数据
const list = ref<ArticleItem[]>([])

//监听数据的变化

watch(
  () => store.channelID,

  async () => {
    const res = await axios.get<ArticleResData>('http://geek.itheima.net/v1_0/articles', {
      params: {
        channel_id: store.channelID,
        timestamp: Date.now()
      }
    })
    // console.log(res.data.data.results)
    list.value = res.data.data.results
  },
  {
    immediate: true
  }
)

const router = useRouter()
//点击去详情页
const toDetail = (id: string) => {
  router.push({
    path: '/detail',
    query: {
      id: id
    }
  })
}
</script>

<template>
  <div class="article-list">
    <div
      class="article-item"
      v-for="item in list"
      :key="item.art_id"
      @click="toDetail(item.art_id)"
    >
      <p class="title">{{ item.title }}</p>
      <img
        v-if="item.cover.type > 0"
        v-for="it in item.cover.images"
        class="img"
        :src="it"
        alt=""
      />
      <div class="info">
        <span>{{ item.aut_name }}</span>
        <span>{{ item.comm_count }}评论</span>
        <span>{{ item.pubdate }}</span>
      </div>
    </div>
  </div>
</template>
